﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Sysbase
{
    public class UserController : BaseController
    {
        // GET: Sysbase/User
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public string Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string userId = WebUtils.GetString("userId");
            string name = WebUtils.GetString("name");
            string departmentCode = WebUtils.GetString("departmentCode");
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfString serviceResult = service.GetUserListForPage(pageIndex, pageSize, userId, name, departmentCode, "");
            var result = "";
            if (serviceResult.S)
            {
                result = serviceResult.D;
            }
            return result;
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            OperateResult resultInfo = new OperateResult();
            tb_User user = new tb_User();
            if (id > 0)
            {
                resultInfo = UserManager.GetUser(id);
                if (resultInfo.Result == 1)
                {
                    user = resultInfo.Body as tb_User;
                }
            }

            ViewBag.User = user;
            return View();
        }

        /// <summary>
        /// 添加/编辑用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(tb_User user)
        {
            ///过滤无效的病区
            string[] codeList = user.DepartmentCode.Split('&');
            string[] nameList = user.DepartmentName.Split('&');
            string newDepartmentCode = "";
            string newDepartmentName = "";
            for (int i = 0; i < nameList.Length; i++)
            {
                if (nameList[i].ToLower() != "null")
                {
                    newDepartmentCode += codeList[i] + "&";
                    newDepartmentName += nameList[i] + "&";
                }
            }
            user.DepartmentCode = newDepartmentCode.TrimEnd('&');
            user.DepartmentName = newDepartmentName.TrimEnd('&');

            OperateResult resultInfo = UserManager.Save(user);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = UserManager.Delete(id);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public ActionResult EditPwd()
        {
            return View();
        }

        /// <summary>
        /// 修改密码保存
        /// </summary>
        /// <returns></returns>
        public ActionResult EditPwdSave()
        {
            string pwd = WebUtils.GetString("password");
            string newPwd = WebUtils.GetString("newPassword");
            OperateResult resultInfo = UserManager.EditPwdSave(pwd, newPwd);
            return JsonDate(resultInfo); ;
        }
    }
}